﻿using System;
using System.Data;
using System.Drawing;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using HiauPdm.Bll;
using HiauPdm.Web;

namespace HiauPdm.Web
{
    public partial class WgjBj : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                TextDh.Attributes["readonly"] = "readonly";
                TextLb.Attributes["readonly"] = "readonly";
                TextGgxh.Attributes["onblur"] = "ExportFzDh(document.getElementById('" + TextDh.ClientID + "').value,this.value);";
                BtnXzdh.OnClientClick = "BrowseDh(document.getElementById('" + TextDh.ClientID + "'),document.getElementById('" + TextLb.ClientID + "'),document.getElementById('" + TextMc.ClientID + "'));return false;";
                BtnAdd.OnClientClick = "if(CheckSave(document.getElementById('" + TextDh.ClientID + "'),document.getElementById('" + TextLb.ClientID + "'),document.getElementById('" + TextMc.ClientID + "'),document.getElementById('" + TextDw.ClientID + "'))){this.disabled=true;" + Page.ClientScript.GetPostBackEventReference(BtnAdd, null) + ";}return false;";
                BindData();
            }
        }

        protected void Pager1_PageChanged(object sender, EventArgs e)
        {
            BindData();
        }

        private void BindData()
        {
            if (Request.QueryString["WgjWhere"] == null)
            {
                PanelList.InnerHtml = "<table cellspacing='0' cellpadding='0' border='0' style='border-style:none;border-collapse:collapse;width:832px;text-align:center;'><tr><td>[未找到外购件记录]</td></tr></table>";
                return;
            }
            string sWgjWhere = Request.QueryString["WgjWhere"].Trim();
            string sWgjDh = string.Empty;
            if (sWgjWhere.IndexOf("FindByWgjDh|") == 0)
            {
                sWgjDh = sWgjWhere.Substring(12);
                sWgjWhere = string.Empty;
            }
            DataSet ds = null;
            if (sWgjWhere != string.Empty || sWgjDh != string.Empty)
            {
                if (!Page.IsPostBack)
                {
                    DataSet dsCount = BWgjBj.GetWgjBjCount(sWgjWhere, sWgjDh);
                    if (dsCount != null)
                    {
                        Pager1.RecordCount = (int)dsCount.Tables[0].Rows[0][0];
                    }
                    if (Request.QueryString["PageIndex"] != null && BGlobal.IsNumeric(Request.QueryString["PageIndex"]))
                    {
                        Pager1.CurrentPageIndex = Convert.ToInt32(Request.QueryString["PageIndex"]);
                    }
                }
                ds = BWgjBj.GetWgjBj(sWgjWhere, sWgjDh, Pager1.PageSize, Pager1.CurrentPageIndex);
            }
            else
            {
                ds = BWgjBj.GetWgjByBjr(IsAccess("编辑所有外购件") ? string.Empty : (string)Session["RealName"]);
            }
            if ((ds == null) || (ds.Tables[0].Rows.Count <= 0))
            {
                PanelList.InnerHtml = "<table cellspacing='0' cellpadding='0' border='0' style='border-style:none;border-collapse:collapse;width:832px;text-align:center;'><tr><td>[未找到外购件记录]</td></tr></table>";
            }
            else
            {
                GridWgj.DataSource = ds;
                GridWgj.DataBind();
                SetCzQx();
                GridWgj.SelectedIndex = -1;
                string sWgjId = Request.QueryString["WgjId"];
                if ((sWgjId != null) && (sWgjId.Trim() != string.Empty))
                {
                    for (int iIndex = 0; iIndex < GridWgj.DataKeys.Count; iIndex++)
                    {
                        if (GridWgj.DataKeys[iIndex].Value.ToString() == sWgjId.Trim())
                        {
                            GridWgj.SelectedIndex = iIndex;
                            break;
                        }
                    }
                }
            }
        }

        private void SetCzQx()
        {
            if (!IsAccess("编辑所有外购件"))
            {
                for (int i = 0; i < GridWgj.DataKeys.Count; i++)
                {
                    if (GridWgj.DataKeys[i].Values[1].ToString() != (string)Session["RealName"])
                    {
                        GridWgj.Rows[i].Cells[8].Controls[3].Visible = false;
                    }
                }
            }
        }

        protected void BtnAdd_Click(object sender, EventArgs e)
        {
            if (!ShowErr(BWgjBj.SaveWgj(TextDh.Text, TextLb.Text, TextMc.Text, TextGgxh.Text, TextDw.Text, TextPpcj.Text, (string)Session["RealName"])))
            {
                Response.Redirect("WgjBj.aspx?WgjWhere=");
            }
        }

        protected void GridWgj_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            if (!ShowErr(BWgjBj.DelWgj((int)GridWgj.DataKeys[e.RowIndex].Value)))
            {
                Response.Redirect(Request.Url.PathAndQuery);
            }
        }
    }
}